/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate.dao;

import hibernate.entity.Courses;
import hibernate.entity.Entities;
import hibernate.entity.EntityTypes;
import java.util.List;

/**
 *
 * @author NDH
 */
public class EventDAO extends AbstractDAO {

    /**
     * @param Entities an string of entity's fields entered to create new one
     * @return int
     * @see Entities
     */
    public int create(Entities c) {
        try {
            tx = session.beginTransaction();
            session.save(c);
            tx.commit();
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * @param Entities an entity choosen to remove from the system
     * @return int
     * @see A list of entities, but the deleted entity
     */
    public int remove(Entities e) {
        try {
            tx = session.beginTransaction();
            e.setStatus(false);
            e = (Entities) session.merge(e);
            session.update(e);
            tx.commit();
            return 1;
        } catch (Exception ex) {
            return 0;
        }
    }

    /**
     * @param name a name to read its corrective entity type
     * @return EntityTypes
     * @see the entity and its info
     */
    public EntityTypes readTypeByName(String name) {
        query = session.createQuery("from EntityTypes where name = :name");
        query.setParameter("name", name);
        return (EntityTypes) query.list().get(0);
    }

    /**
     * @return List<Entities>
     * @see A whole list of entities
     */
    public List<Entities> readAll() {
        query = session.createQuery("from Entities where entityTypes.name = 'event'");
        return query.list();
    }

    /**
     * @param id an id entered to fill and retrieve the course
     * @return Courses
     * @see Courses
     */
    public Courses readByID(int id) {
        try {
            query = session.createQuery("from Entities where entityTypes.name = 'event' and entityId= :id");
            query.setParameter("id", id);
            return (Courses) query.list().get(0);
        } catch (Exception e) {
            return null;
        }

    }

    /**
     * @return List<Entities>
     * @see List of Event
     */
    public List<Entities> readLiving() {
        // and a.status = true
        query = session.createQuery("from Entities e where  e.entityTypes.name = 'Event' and e.status = 'true'");
        return query.list();
    }

    /**
     * @param Courses a course's fields to create new one
     * @return int
     * @see List of Courses
     */
    public int create(Courses c) {
        try {
            tx = session.beginTransaction();
            session.save(c);
            tx.commit();
            return 1;
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * @param Entities a entities's fields to update edited one
     * @return int
     * @see List of Entities
     */
    public int update(Entities c) {
        try {
            tx = session.beginTransaction();
            c = (Entities) session.merge(c);
            session.saveOrUpdate(c);
            tx.commit();
            return 1;
        } catch (Exception e) {
            return 0;
        }

    }

    /**
     * @param keyword a word entered from the keyboard by user to search
     * @return List of entities
     * @see List of entities
     */
    public List<Entities> search(String keyword) {
        query = session.createQuery("from Entities e where  e.entityTypes.name = 'Event' and e.status = 'true' "
                + "or e.title like '%" + keyword + "%' "
                + "or e.content like '%" + keyword + "%' ");

        return query.list();
    }
}
